package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.OrderNumberListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.OrderNumberList;
import com.isunland.managesystem.entity.OrderNumberList.OrderNumberDetail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 出库订单列表
 *
 * @author wu
 */
public class OrderNumberListFragment extends BaseListFragment {

  protected static final String REQUEST_ORDER_NUMBER =
      "com.isunland.managesystem.ui.REQUEST_ORDER_NUMBER";
  protected static final String REQUEST_ORDER_ID = "com.isunland.managesystem.ui.REQUEST_ORDER_ID";
  protected static final String REQUEST_CONTRACT_NAME =
      "com.isunland.managesystem.ui.REQUEST_CONTRACT_NAME";
  protected static final String REQUEST_CONTRACT_ID =
      "com.isunland.managesystem.ui.REQUEST_CONTRACT_ID";
  protected static final String REQUEST_CUSTOMER_NAME =
      "com.isunland.managesystem.ui.REQUEST_CUSTOMER_NAME";
  protected static final String REQUEST_ADDRESS_NAME =
      "com.isunland.managesystem.ui.REQUEST_ADDRESS_NAME";
  protected static final String REQUEST_MAIN_GOODS =
      "com.isunland.managesystem.ui.REQUEST_MAIN_GOODS";
  protected static final String REQUEST_CUSTOMER_ID =
      "com.isunland.managesystem.ui.REQUEST_CUSTOMER_ID";
  private OrderNumberListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.outputOrderDetail);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_ORDERNUMBER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode  ", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("status", "outStock,confirm");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    OrderNumberList orderNumber = new Gson().fromJson(arg0, OrderNumberList.class);
    List<OrderNumberDetail> orderDetail = orderNumber.getRows();
    setAdapter(orderDetail);
  }

  private void setAdapter(List<OrderNumberDetail> orderDetail) {
    if (orderDetail == null) {
      orderDetail = new ArrayList<OrderNumberList.OrderNumberDetail>();
    }

    if (mAdapter == null) {
      mAdapter = new OrderNumberListAdapter(getActivity(), orderDetail);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(orderDetail);

    ((OrderNumberListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    OrderNumberDetail detail = mAdapter.getItem(position - 1);
    String orderNumber = detail.getMoutbillNo();
    String orderId = detail.getId();
    String contractName = detail.getMcontractName();
    String contractId = detail.getMcontractId();
    String customerName = detail.getMcustomerName();
    String customerId = detail.getMcustomerId();
    String addressName = detail.getMcustomerAddress();
    String mainGoods = detail.getMoutCom();
    Intent intent = new Intent();
    intent.putExtra(REQUEST_ORDER_NUMBER, orderNumber);
    intent.putExtra(REQUEST_ORDER_ID, orderId);
    intent.putExtra(REQUEST_CONTRACT_NAME, contractName);
    intent.putExtra(REQUEST_CONTRACT_ID, contractId);
    intent.putExtra(REQUEST_CUSTOMER_NAME, customerName);
    intent.putExtra(REQUEST_CUSTOMER_ID, customerId);
    intent.putExtra(REQUEST_ADDRESS_NAME, addressName);
    intent.putExtra(REQUEST_MAIN_GOODS, mainGoods);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
